package com.demo.first;

import com.demo.dao.BookDao;
import com.demo.dao.impl.BookDaoImpl;
import com.demo.pojo.Book;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.List;

public class IndexManager {
    @Test
    public void createIndex() throws Exception {
        // 采集数据
        BookDao dao = new BookDaoImpl();
        List<Book> list = dao.queryBooks();

        // 将采集到的数据封装到Document对象中
        List<Document> docList = new ArrayList<>();
        Document document;
        for (Book book : list) {
            document = new Document();
            // store:如果是yes，则说明存储到文档域中
            // 图书ID
            // Field id = new TextField("id", book.getId().toString(), Store.YES);

            Field id = new TextField("id", Integer.toString(book.getId()), Field.Store.YES);
            // 图书名称
            Field name = new TextField("name", book.getName(), Field.Store.YES);
            // 图书价格
            Field price = new TextField("price", Float.toString(book.getPrice()), Field.Store.YES);
            // 图书图片地址
            Field pic = new TextField("pic", book.getPic(), Field.Store.YES);
            // 图书描述
            Field description = new TextField("description", book.getDescription(), Field.Store.YES);

            // 将field域设置到Document对象中
            document.add(id);
            document.add(name);
            document.add(price);
            document.add(pic);
            document.add(description);

            docList.add(document);
        }

        //JDK 1.7以后 open只能接收Path/////////////////////////////////////////////////////

        // 创建分词器，标准分词器
        Analyzer analyzer = new StandardAnalyzer();

        // 创建IndexWriter
        // IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_6_5_0,analyzer);
        IndexWriterConfig cfg = new IndexWriterConfig(analyzer);

        // 指定索引库的地址
//         File indexFile = new File("D:\\L\a\Eclipse\\lecencedemo\\");
//         Directory directory = FSDirectory.open(indexFile);
        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:\\lucenceIndex1\\"));

        IndexWriter writer = new IndexWriter(directory, cfg);
        writer.deleteAll(); //清除以前的index
        // 通过IndexWriter对象将Document写入到索引库中
        for (Document doc : docList) {
            writer.addDocument(doc);
        }

        // 关闭writer
        writer.close();
    }
}
